package com.winbomb.kingcard.base.anim.impl;

import android.graphics.Color;
import android.graphics.Paint;

import com.winbomb.kingcard.Assets;
import com.winbomb.kingcard.IGameLogic;
import com.winbomb.kingcard.IPlayer;
import com.winbomb.kingcard.Pos;
import com.winbomb.kingcard.base.GameLogic;
import com.winbomb.kingcard.base.anim.IPlayable;
import com.winbomb.kingcard.framework.Graphics;

public class BattleResultAnim implements IPlayable {
	private static final Paint BLUE_PAINT = new Paint();
	private static final Paint RED_PAINT = new Paint();

	private float delay;
	private Pos winPos;
	private Pos losePos;
	private int winCnt;
	private int loseCnt;
	private Graphics g;

	public BattleResultAnim(IGameLogic game) {
		this.g = ((GameLogic) game).getRender().getGraphics();

		BLUE_PAINT.setColor(Color.BLUE);
		BLUE_PAINT.setTextSize(18);
		RED_PAINT.setColor(Color.RED);
		RED_PAINT.setTextSize(18);
	}

	public void setWinner(IPlayer winner, int winCnt) {
		this.winPos = winner.getPlayerInfo().getFramePos();
		this.winCnt = winCnt;
	}

	public void setLoser(IPlayer loser, int loseCnt) {
		this.losePos = loser.getPlayerInfo().getFramePos();
		this.loseCnt = loseCnt;
	}

	public void reset() {
		this.delay = 0;
		this.winPos = null;
		this.losePos = null;
		this.winCnt = 0;
		this.loseCnt = 0;
	}

	@Override
	public boolean play(float dt) {

		g.drawPixmap(Assets.actions, winPos.x, winPos.y, 0, 154, 48, 22);
		g.drawPixmap(Assets.actions, losePos.x, losePos.y, 0, 154, 48, 22);

		// 绘制胜败
		if (delay <= 1000f) {

			g.drawPixmap(Assets.battle_result, losePos.x + 2, losePos.y + 2, 178, 0, 46, 21);
			g.drawPixmap(Assets.battle_result, winPos.x + 2, winPos.y + 2, 178, 21, 46, 21);

			delay += dt;
			return false;
		}

		// 绘制兵力变化
		else if (delay <= 2500f) {

			int winBaseY = (winCnt < 0) ? 0 : 21;
			int loseBaseY = (loseCnt < 0) ? 0 : 21;

			g.drawPixmap(Assets.battle_result, winPos.x + 6, winPos.y + 2, 0, winBaseY, 16, 21);
			g.drawPixmap(Assets.battle_result, losePos.x + 6, losePos.y + 2, 0, loseBaseY, 16, 21);

			int winNumX = 16 + Math.abs(winCnt) * 16;
			int loseNumX = 16 + Math.abs(loseCnt) * 16;

			g.drawPixmap(Assets.battle_result, winPos.x + 26, winPos.y + 2, winNumX, winBaseY, 16, 21);
			g.drawPixmap(Assets.battle_result, losePos.x + 26, losePos.y + 2, loseNumX, loseBaseY, 16, 21);

			delay += dt;
			return false;
		}

		return true;
	}
}
